# @Time:        2024/7/7 上午10:48
# @USER:        lihuizhuo
# @Site:        www.swust.edu.cn
# @File:        M
# @Software:    PyCharm
# @license:     Copyright(C), swust
# @Author:      huizhuo
# @Email:       huizhuoli@foxmail.com
# @Version:     V1.1.0


import os
import sys
import pygame
from pygame import Surface
from pytmx import TiledObjectGroup

parent_dir = os.path.dirname(
    os.path.dirname(os.path.abspath(__file__)))
print(parent_dir)
sys.path.insert(0, parent_dir)

# sys.path.insert(0, "/Users/lihuizhuo/PycharmProjects/2024-edu-prj/demo2")

from actor.soldier import ShootSoldier, WalkSoldier
from scene.tiled_render import TiledRenderer


def create_sprite(tmx_map):
    shoot_group = pygame.sprite.Group()
    obstacle_group = pygame.sprite.Group()
    work_soldier = None

    for layer in tmx_map.tmx_data.visible_layers:
        if isinstance(layer, TiledObjectGroup):
            if layer.name == '小铁':
                for obj in layer:
                    if obj.name == '小铁':
                        work_soldier = WalkSoldier("resources/images/boy1.png",
                                                   obj.x, obj.y,
                                                   obj.name, 100)

            if layer.name == "日本人":
                for obj in layer:
                    shoot_soldier = ShootSoldier("resources/images/chinese/red_army/red_army1.png",
                                                 "resources/images/chinese/red_army/red_army1-1.png",
                                                 obj.x, obj.y, "战士", 100)

                    shoot_group.add(shoot_soldier)
            if layer.name == "障碍物":
                for obj in layer:
                    obstacle = pygame.sprite.Sprite()
                    obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    obstacle_group.add(obstacle)

    return work_soldier, shoot_group, obstacle_group


def game_run():
    """
    游戏运行
    :return:
    """
    # pygame setup
    pygame.init()
    # set up the display 窗口
    screen = pygame.display.set_mode((1280, 720))
    clock = pygame.time.Clock()
    running = True

    tmx_map = TiledRenderer("resources/tmx/tunnel_map.tmx")
    work_soldier, group, obstacle_group = create_sprite(tmx_map)
    background_surface = Surface((tmx_map.tmx_data.tilewidth * tmx_map.tmx_data.width,
                                  tmx_map.tmx_data.tileheight * tmx_map.tmx_data.height))

    tmx_map.render_map(background_surface)
    background_x = 0
    background_y = 0

    while running:
        # poll for events
        # pygame.QUIT event means the user clicked X to close your window
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                work_soldier.run(event.key)
                if event.key == pygame.K_LEFT:
                    background_x -= 10
                elif event.key == pygame.K_RIGHT:
                    background_x += 10
                elif event.key == pygame.K_UP:
                    background_y -= 10
                elif event.key == pygame.K_DOWN:
                    background_y += 10

                # TODO: 边界判断

        temp_surface = background_surface.subsurface((background_x, background_y, 1280, 720))

        screen.blit(temp_surface, (0, 0))
        work_soldier.draw(surface=screen, offset_x=background_x, offset_y=background_y)

        for sprite in group:
            sprite.draw(screen, background_x, background_y)

        pygame.display.flip()

        clock.tick(20)  # limits FPS to 60

    pygame.quit()


if __name__ == '__main__':
    # print(ShootDir.not_shoot)
    game_run()
